package com.bdzl.biz.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.bdzl.biz.service.OrderVehService;

import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.loan.OrderVehDO;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.data.VehModelVO;
import com.bdzl.pub.vo.loan.OrderVehVO;
import org.springframework.stereotype.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;


/**
 * 贷款主订单车辆信息表：用于描述，贷款订单所租车型、车价及数量信息
 * 
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-05-11 11:58:12
 */
 
@Controller
@RequestMapping("/bdzl/orderVeh")
public class OrderVehController extends BaseController {
	@Autowired
	private OrderVehService orderVehService;
	
	@GetMapping()
	String OrderVeh(){
	    return "bdzl/orderVeh/orderVeh";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OrderVehDO> orderVehList = orderVehService.list(query);
		int total = orderVehService.count(query);
		PageUtils pageUtils = new PageUtils(orderVehList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "bdzl/orderVeh/add";
	}


	@GetMapping("/edit/{vehId}")
	@ResponseBody
	R edit(@PathVariable("vehId") String vehId){
		Map map = new HashMap();
		map.put("vehId",vehId);
		List<OrderVehVO> orderVehList = orderVehService.listAndUser(map);
	  return  R.ok(orderVehList.get(0));
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(OrderVehDO orderVeh){
		if(orderVehService.save(orderVeh)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( OrderVehDO orderVeh){
		orderVehService.update(orderVeh);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( String vehId){
		if(orderVehService.remove(vehId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") String[] vehIds){
		orderVehService.batchRemove(vehIds);
		return R.ok();
	}


	@ResponseBody
	@GetMapping("/listAndUser")
	public PageUtils listAndUser(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<OrderVehVO> orderVehList = orderVehService.listAndUser(query);
		int total = orderVehService.count(query);
		PageUtils pageUtils = new PageUtils(orderVehList, total);
		return pageUtils;
	}

	// 导出
	@GetMapping("download")
	public void download(HttpServletResponse response) throws IOException {
		//获取要导出的list对象
		List<OrderVehVO> orderVehList = orderVehService.listAndUser(null);
		// 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
		String fileName = URLEncoder.encode("车辆信息", "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		//   Student.class 是按导出类  data()应为数据库查询数据，这里只是模拟
		EasyExcel.write(response.getOutputStream(), OrderVehVO.class).sheet("车辆信息").doWrite(orderVehList);
	}

}
